package pers.lls.hds.sort.recursion;


/**
 * 二分查找
 *
 * @author lilichai
 */
public class BinarySearch {

    long[] arr = {1,3,5,2,6,8,4,9,7};
    int size = arr.length;

    public int find(long searchKey) {
        return recFind(searchKey, 0, size - 1);
    }

    private int recFind(long searchKey, int lowerBound, int upperBound) {
        int curIn;
        curIn = (lowerBound + upperBound) / 2;
        if (arr[curIn] == searchKey) {
            return curIn;
        } else if (lowerBound > upperBound) {
            return size;
        } else {
            if (arr[curIn] < searchKey) {
                return recFind(searchKey, curIn + 1, upperBound);
            } else {
                return recFind(searchKey, lowerBound, curIn - 1);
            }
        }
    }

    public static void main(String[] args) {
        BinarySearch binarySearch = new BinarySearch();
        System.out.println(binarySearch.find(4));
    }


}

